/*
 *    Copyright (c) 2018-2025, sloth All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: sloth
 */

package cn.s1995.sloth.pay.biz.controller;

import cn.s1995.sloth.bizs.oo.req.OrderRefundVerifyREQ;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.util.SecurityUtils;
import cn.s1995.sloth.pay.biz.oo.req.PayParamREQ;
import cn.s1995.sloth.pay.biz.oo.vo.PayParamVO;
import cn.s1995.sloth.pay.biz.service.BizPayService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 产品
 *
 * @author sloth
 * @date 2019-05-28 23:58:27
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/biz/pay")
@Tag(description = "PayController", name = "PAY-PLATFORM-BIZ-支付")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PayController {

	private final BizPayService bizPayService;

	@ApiOperationSupport(order = 10)
	@Operation(summary = "创建支付单", description = "创建支付单")
	@PostMapping("/order")
	public R<PayParamVO> createOrder(@RequestBody @Validated PayParamREQ req) {
		req.setUid(SecurityUtils.getUser().getId());
		return R.ok(bizPayService.createOrder(req));
	}

	@ApiOperationSupport(order = 20)
	@Operation(summary = "捕获支付单", description = "捕获支付单")
	@Parameter(name = "payFlowId", description = "支付流水id", in = ParameterIn.PATH)
	@PostMapping("/{payFlowId}/capture")
	public R<PayParamVO> captureOrder(@PathVariable String payFlowId) {
		return R.ok(bizPayService.captureOrder(payFlowId, Boolean.FALSE));
	}

	@ApiOperationSupport(order = 30)
	@Operation(summary = "查询支付结果", description = "查询支付结果")
	@Parameter(name = "payFlowId", description = "支付流水id", in = ParameterIn.PATH)
	@GetMapping("/order/{payFlowId}")
	public R<PayParamVO> queryOrder(@PathVariable String payFlowId) {
		return R.ok(bizPayService.queryOrder(payFlowId));
	}

	@ApiOperationSupport(order = 40)
	@PostMapping("/refund/{refundId}")
	@Parameter(name = "refundId", description = "退款id", in = ParameterIn.PATH)
	@Operation(summary = "退款", description = "退款")
	public R refund(@PathVariable String refundId,@ParameterObject OrderRefundVerifyREQ req) {
		bizPayService.refund(refundId, req);
		return R.ok();
	}


}
